package ca.venasse.pinventory.inventory;

import org.axonframework.eventhandling.annotation.EventHandler;
import org.axonframework.eventsourcing.annotation.AbstractAnnotatedEntity;

import ca.venasse.pinventory.common.Quantity;
import ca.venasse.pinventory.inventory.common.PartIdentifier;
import ca.venasse.pinventory.inventory.event.LossRecorded;
import ca.venasse.pinventory.inventory.event.PartPurchased;
import ca.venasse.pinventory.inventory.event.RecordedLossReversed;

public class Part extends AbstractAnnotatedEntity {

	private final PartIdentifier partIdentifier;
	private Quantity quantity;

	public Part(PartIdentifier partIdentifier) {
		this.partIdentifier = partIdentifier;
		this.quantity = Quantity.create(0);
	}

	@EventHandler
	protected void z_handle(LossRecorded event) {
		if (!this.partIdentifier.equals(event.getPartIdentifier())) {
			return;
		}

		this.quantity = this.quantity.less(event.getQuantity());
	}

	@EventHandler
	protected void z_handle(PartPurchased event) {
		if (!this.partIdentifier.equals(event.getPartIdentifier())) {
			return;
		}

		this.quantity = this.quantity.plus(event.getQuantity());
	}

	@EventHandler
	protected void z_handle(RecordedLossReversed event) {
		if (!this.partIdentifier.equals(event.getPartIdentifier())) {
			return;
		}

		this.quantity = this.quantity.plus(event.getQuantity());
	}

}
